/// @author: cairuoyu
/// @homepage: http://cairuoyu.com
/// @github: https://github.com/cairuoyu/flutter_admin
/// @date: 2021/6/21
/// @version: 1.0
/// @description:

import 'package:flutter_admin/api/odoo/base_teampath_odoo_api.dart';
import 'package:flutter_admin/models/odoo/base_team.dart';
import 'package:flutter_admin/models/odoo/base_teampath.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/generated/l10n.dart';

class TeamPathSelectList extends StatefulWidget {
  TeamPathSelectList({
    Key? key,
    required this.baseTeam,
    this.isSelected = false,
    this.title,
  }) : super(key: key);
  final BaseTeam? baseTeam;
  final bool isSelected;
  final String? title;

  @override
  TeamPathSelectListState createState() => TeamPathSelectListState();
}

class TeamPathSelectListState extends State<TeamPathSelectList> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  PageModel page =
      PageModel(orders: [OrderItemModel(column: 'create_date', asc: false)]);
  BaseTeamPath baseTeamPath = BaseTeamPath();
  List<BaseTeamPath> selectedList = [];
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      query();
    });
  }

  @override
  Widget build(BuildContext context) {
    var buttonBar = CryButtonBar(
      children: [
        CryButton(
          label: S.of(context).query,
          iconData: Icons.search,
          padding: EdgeInsets.only(left: 20),
          onPressed: () {
            formKey.currentState!.save();
            this.query();
          },
        ),
        CryButton(
          label: S.of(context).reset,
          iconData: Icons.refresh,
          padding: EdgeInsets.only(left: 20),
          onPressed: () {
            baseTeamPath = BaseTeamPath();
            formKey.currentState!.reset();
            this.baseTeamPath.processName = null;
            this.query();
          },
        ),
      ],
    );
    var form = Form(
      key: formKey,
      child: Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        children: <Widget>[
          CryInput(
            width: 400,
            label: '工序名称',
            value: baseTeamPath.processName,
            onSaved: (v) {
              baseTeamPath.processName = v;
            },
          ),
          SizedBox(child: buttonBar, width: 260),
        ],
      ),
    );

    CryDataTable table = CryDataTable(
      key: tableKey,
      title: widget.title!,
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        query();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        query();
      },
      onSelectChanged: (Map selected) {
        this.setState(() {});
      },
      columns: [
        DataColumn(
          label: Container(child: Text('工序名称')),
          // onSort: (int columnIndex, bool ascending) => _sort('userName'),
        ),
      ],
      getCells: (m) {
        BaseTeamPath baseStationPath = BaseTeamPath.fromMap(m);
        return [
          DataCell(Text(baseStationPath.processName ?? '--')),
        ];
      },
    );
    var result = Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          form,
          Row(children: [
            Expanded(
              child: table,
            )
          ]),
        ],
      ),
    );
    return result;
  }

  List<BaseTeamPath> getSelectedList() {
    List<BaseTeamPath> selectedList = tableKey.currentState
            ?.getSelectedList(page)
            .map<BaseTeamPath>((e) => BaseTeamPath.fromMap(e))
            .toList() ??
        [];
    return selectedList;
  }

  query() async {
    //Map params = {'roleId': widget.role!.id, 'userName': this.userInfo.name};
    // RequestBodyApi requestBodyApi = RequestBodyApi();
    //requestBodyApi.page = page;
    // requestBodyApi.params = params;
    //  ResponseBodyApi responseBodyApi;
    if (widget.isSelected) {
      //已选择
      baseTeamPath.teamId = widget.baseTeam!.id;
      ResponseBodyApi responseBodyApi =
          await BaseTeampathOdooApi.page(data: {
        "page": page,
        "domainList": baseTeamPath.toDomainList()
      });
      if (responseBodyApi.success!) {
        page =
            responseBodyApi.data != null ? responseBodyApi.data : PageModel();
        tableKey.currentState!.loadData(page);
      }
    } else {
      //未选择数据
      // baseStationuser.departmentId = widget.baseProductionstation!.workshopId;
      // ResponseBodyApi responseBodyApi = await BaseTeampathOdooApi.processList();
      // if (responseBodyApi.success!) {
      //   baseTeamPath.processList = responseBodyApi.data;
      //   // baseStationPath.departmentId = widget.baseProductionstation!.workshopId;
      //   responseBodyApi = await BaseTeampathOdooApi.processPage(
      //       data: {"page": page, "domainList": baseTeamPath.toProcessDomainList()});
      //   if (responseBodyApi.success!) {
      //     page =
      //         responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      //     tableKey.currentState!.loadData(page);
      //   }
      // }
      ResponseBodyApi responseBodyApi = await BaseTeampathOdooApi.processPage(
          data: {"page": page, "domainList": baseTeamPath.toProcessDomainList()});
      if (responseBodyApi.success!) {
        page =
          responseBodyApi.data != null ? responseBodyApi.data : PageModel();
        tableKey.currentState!.loadData(page);
      }
    }

    tableKey.currentState!.loadData(page);
  }
}
